package com.tiantian.quanminxiu.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.presenter.MainMinePresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.FragmentBase;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.MainMineView;

import rx.functions.Action1;

/*
* 首页
* */
public class Fragment_Main_Mine extends FragmentBase implements MainMineView{

    private MainMinePresent present;

    private View vMineAuthen,vMineRecharge,vMineAccept,vMineLogout;
    private ImageView imgUserAvatar;
    private TextView txtUserName,txtMyEarn,txtMineVersion,txtMineCoin,txtUserID;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_mine, container, false);

        Init(view);
        InitView(view);

        present = new MainMinePresent();
        present.attachView(this);

        UserData.Reload.Fragment_Mine = true;
        return view;
    }
    //endregion

    public void InitView(View view){
        vMineAuthen = view.findViewById(R.id.vMineAuthen);
        vMineRecharge = view.findViewById(R.id.vMineRecharge);
        vMineAccept = view.findViewById(R.id.vMineAccept);
        vMineLogout = view.findViewById(R.id.vMineLogout);

        txtMineVersion = (TextView) view.findViewById(R.id.txtMineVersion);
        txtUserID = (TextView) view.findViewById(R.id.txtUserID);
        txtMineCoin = (TextView) view.findViewById(R.id.txtMineCoin);
        txtUserName = (TextView) view.findViewById(R.id.txtUserName);
        txtMyEarn = (TextView) view.findViewById(R.id.txtMyEarn);

        imgUserAvatar = (ImageView) view.findViewById(R.id.imgUserAvatar);

        RxView.clicks(vMineAuthen).map(View->vMineAuthen).subscribe(OC_Button);
        RxView.clicks(vMineRecharge).map(View->vMineRecharge).subscribe(OC_Button);
        RxView.clicks(vMineLogout).map(View->vMineLogout).subscribe(OC_Button);
        RxView.clicks(imgUserAvatar).map(View->imgUserAvatar).subscribe(OC_Button);
        RxView.clicks(vMineAccept).map(View->vMineAccept).subscribe(OC_Button);
        RxView.clicks(txtUserName).map(View->txtUserName).subscribe(OC_Button);
    }

    //region PageInit()
    private void PageInit() {
        if (!UserData.Reload.Fragment_Mine) {
            return;
        }
        UserData.Reload.Fragment_Mine = false;
        //////////////////////////首页信息////////////////////////////
        imgUserAvatar.post(new Runnable() {
            @Override
            public void run() {
                APPUtils.displayImage(APPUtils.createUserAvatar(UserData.Base.User_ID), imgUserAvatar,thisActivity, imgUserAvatar.getHeight() / 2);
            }
        });
        txtUserName.setText(UserData.Base.NickName);
        txtMineVersion.setText(UserData.Current.APP_Version);
        txtUserID.setText("全民秀ID:"+UserData.Base.User_ID);
        txtMyEarn.setText(UserData.Base.Beans+"金豆");
        txtMineCoin.setText(UserData.Base.Coin+"金币");
    }
    //endregion

    private Action1<View> OC_Button = v->{
        switch (v.getId()){
            case R.id.txtHeader_Edit:
            case R.id.imgUserAvatar:
            case R.id.txtUserName:
            case R.id.vMineAccept:
                JumpEditInfo();
                break;
            case R.id.vMineRecharge:
                JumpRecharge();
                break;
            case R.id.vMineAuthen:
                if(UserData.Base.isAnchor){
                    CommonUtils.showToask(thisContext,"你已经是艺人了...");
                    return;
                }
                APPUtils.Authentication_TO(thisActivity);
                break;
            case R.id.vMineLogout:
                present.logout();
                break;
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        PageInit();
    }

    @Override
    public void logout() {
        CommonUtils.showToask(thisContext,"退出成功");
        APPUtils.LoginSelect_To(thisActivity);
        thisActivity.finish();
    }

    @Override
    public void JumpEditInfo() {
        APPUtils.EditInfo_TO(thisActivity);
    }

    @Override
    public void JumpRecharge() {
        APPUtils.Recharge_TO(thisActivity);
    }
}